using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SunshineFarmWord
{
    public class SystemTimeManage : MonoBehaviour
    {
        public class TimeEvent
        {
            public float time;
            public Action action;
            public int id;
            public TimeEvent(float time, Action action, int id)
            {
                this.time = time;
                this.action = action;
                this.id = id;
            }
        }
        //事件List
        private List<TimeEvent> _timeEventList = new List<TimeEvent>();
        private int _eventId = 0;
        //单利
        public static SystemTimeManage Instance { get; private set; }

        private int gameSecond, gameMinute, gameHour, gameDay, gameMonth, gameYear;
        //时间
        private float _time;
        // 游戏时钟暂停
        private bool gameClockPause;

        private void Awake()
        {
            Instance = this;

            InitData();
        }
        void Start()
        {
        }

        void Update()
        {
            if (gameClockPause) return;
            // 计时
            _time += Time.deltaTime;
            if (_time >= 1) AddNumber();
            // 执行事件
            UpdateTimeEvent();
        }

        #region Main function
        private void InitData()
        {
            gameSecond = 0;
            gameMinute = 0;
            gameHour = 0;
            gameDay = 0;
            gameMonth = 0;
            gameYear = 0;
            _time = 0;
            gameClockPause = false;
        }

        private void AddNumber()
        {
            _time = 0;
            gameSecond++;
            if (gameSecond >= 60)
            {
                gameSecond = 0;
                gameMinute++;
            }
            if (gameMinute >= 60)
            {
                gameMinute = 0;
                gameHour++;
            }
            if (gameHour >= 24)
            {
                gameHour = 0;
                gameDay++;
            }
            if (gameDay >= 30)
            {
                gameDay = 0;
                gameMonth++;
            }
            if (gameMonth >= 12)
            {
                gameMonth = 0;
                gameYear++;
            }
            if (gameYear >= 999)
            {
                gameYear = 0;
            }
        }

        private void UpdateTimeEvent()
        {
            if (_timeEventList.Count == 0) return;
            for (int i = 0; i < _timeEventList.Count; i++)
            {
                TimeEvent timeEvent = _timeEventList[i];
                if (timeEvent.time <= 0)
                {
                    timeEvent.action?.Invoke();
                    _timeEventList.RemoveAt(i);
                    i--;
                }
                else
                {
                    timeEvent.time -= Time.deltaTime;
                }
            }
        }
        #endregion

        #region public function 
        public int AddTimeEvent(float time, Action action)
        {
            int currentId = _eventId++;
            _timeEventList.Add(new TimeEvent(time, action, currentId));

            // 排序
            _timeEventList.Sort((x, y) => x.time.CompareTo(y.time));

            return currentId;
        }

        public void RemoveTimeEvent(int id)
        {
            for (int i = 0; i < _timeEventList.Count; i++)
            {
                if (_timeEventList[i].id == id)
                {
                    _timeEventList.RemoveAt(i);
                    return;
                }
            }
        }
        #endregion

        #region 暂停方法
        //时间暂停/恢复
        public void PauseGameClock()
        {
            gameClockPause = true;
            Time.timeScale = 0;
        }
        public void ResumeGameClock()
        {
            gameClockPause = false;
            Time.timeScale = 1;
        }
        public bool GetGameClockPause()
        {
            return gameClockPause;
        }
        #endregion
    }
}
